// $Id: nodepartition.cpp 72 2004-12-02 15:03:28Z jonas $
#include "nodepartition.h"
#include "partitionizer.h"

/**
 * @todo Write documentation
 */
NodePartition::NodePartition()
{
}

/**
 * @todo Write documentation
 * @param partitionLimit This is the slowest partition members that should be added to the list
 */
void NodePartition::discriminateRequestOrderListPartition(std::list<KeyDistanceHelper> &list, int partitionLimit)
{
	// Discriminate slow nodes
	//int thisPartition = Partitionizer::getInstance()->getPartition(this);		/// @todo Think aboit this interesting idea: You could use the difference between the current nodes partition and the partition of the node in the routing table to discriminate. That way you could discriminate *only* the slower nodes, getting a more equal load and better routing from the faster nodes.
	int partition;
	for (std::list<KeyDistanceHelper>::iterator kdHelper=list.begin(); kdHelper!=list.end();) {
		// Check if the node is fast enough for the partitionLimit
		partition = Partitionizer::getInstance()->getPartition((*kdHelper).m_entry->m_node);
		if(partition <= partitionLimit){
			(*kdHelper).m_distance = ((*kdHelper).m_distance/(Partitionizer::getInstance()->getPartitionCount())) + ((RAND_MAX/(Partitionizer::getInstance()->getPartitionCount())) * (partition) );
			kdHelper++;
		}
		else{
			std::list<KeyDistanceHelper>::iterator kdHelperTemp = kdHelper++;
			list.erase(kdHelperTemp);
		}
	}

	// Sort the list
	list.sort(SortKeyDistanceHelper());
}
